//
//  ScreenTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/10.
//

import UIKit

class ScreenTableViewCell: UITableViewCell {
    var collectionView: UICollectionView!
    var dataArray = NSMutableArray()//品牌
    override init(style: UITableViewCellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.clipsToBounds = true
        self.backgroundColor = .white
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 10
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 14, y: 0, width: kScreenWidth - 28, height: 50), collectionViewLayout: layout)
        self.collectionView.register(ScreenCollectionViewCell.self, forCellWithReuseIdentifier: "ScreenCollectionViewCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = .white
        self.collectionView.showsHorizontalScrollIndicator = false
        self.contentView.addSubview(self.collectionView)
        
        self.collectionView.snp.makeConstraints { make in
            make.top.equalTo(5)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(32)
            make.bottom.equalTo(-20)
        }
        
    }
    
    //setModel
    func setData(data: NSMutableArray){
        self.dataArray = data
        self.collectionView.reloadData()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension ScreenTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        var tempWidth = 90.0
        if self.dataArray.count > indexPath.row{
            if let contetModel = self.dataArray[indexPath.row] as? BrandSeriesListModel {
                tempWidth = XTManageCenter.getLabWidth(labelStr: contetModel.brandName ?? "", font: UIFont.init(name: Theme.PingFangSCRegular, size: 12)!, height: 32)
                return CGSize.init(width: tempWidth + 30, height: 32)
            }
        }
        return CGSize.init(width: tempWidth, height: 32)
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ScreenCollectionViewCell", for: indexPath) as! ScreenCollectionViewCell
        if self.dataArray.count > indexPath.row{
            let model = self.dataArray[indexPath.row] as! BrandSeriesListModel
            cell.setBrandModel(model: model)
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if self.dataArray.count > 0{
            let model = self.dataArray[indexPath.row] as! BrandSeriesListModel
            model.isSelect = !model.isSelect
            for index in 0 ..< self.dataArray.count{
                let obj = self.dataArray[index] as! BrandSeriesListModel
                if model.id == obj.id {
                    model.isSelect = true
                }else{
                    obj.isSelect = false
                }
            }
        }
        self.collectionView.reloadData()
    }
}

